package com.spring.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.SneakyThrows;

/**
 * @Author: lzx
 * @Data: 2021/04/26/16:30
 * @Description：
 */
public class RabbitMQUtils {

    private static ConnectionFactory factory ;
    static{
        //重量级资源 类加载只执行一次
        factory = new ConnectionFactory();
        factory.setHost("ali");
        factory.setPort(5672);
        factory.setUsername("lingzx");
        factory.setPassword("lingzx");
        factory.setVirtualHost("/elasticsearch");
    }
    // 获取连接工厂
    @SneakyThrows
    public static Connection getConnection() {
        return factory.newConnection();
    }
    //关闭连接
    @SneakyThrows
    public static void closeConnectionAndChannel(Connection connection, Channel channel) {
        channel.close();
        connection.close();
    }
}
